package com.zlw.producet;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;
import java.util.UUID;

/**
 * @author Zhaoluowei
 * @version 1.0
 * @date 2020/1/2 14:46
 */
@Slf4j
@Component
public class RabbitSender {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    //确认消息发送
    final RabbitTemplate.ConfirmCallback confirmCallback = new RabbitTemplate.ConfirmCallback() {
        @Override
        public void confirm(CorrelationData correlationData, boolean ack, String cause) {
            log.info("confirmCallback confirm --> correlationData " + correlationData);
            log.info("confirmCallback confirm --> ack " + ack);
            log.info("confirmCallback confirm --> cause " + cause);
            if (!ack) {
                log.info(" do 异常处理 ...");
            }

        }
    };


    final RabbitTemplate.ReturnCallback returnCallback = new RabbitTemplate.ReturnCallback() {
        @Override
        public void returnedMessage(org.springframework.amqp.core.Message message, int replyCode,
                                    String replyText, String exchange, String routingKey) {
            log.error(" ---------------------------------------------------- ");
            log.info("confirmCallback returnedMessage --> message " + message);
            log.info("confirmCallback returnedMessage --> replyCode " + replyCode);
            log.info("confirmCallback returnedMessage --> replyText " + replyText);
            log.info("confirmCallback returnedMessage --> exchange " + exchange);
            log.info("confirmCallback returnedMessage --> routingKey " + routingKey);


        }
    };

    public void send(Object message, Map<String, Object> properties) throws Exception {
        log.info("RabbitSender  --  send 测试 交换机是 topic 类型 ");
        MessageHeaders messageHeaders = new MessageHeaders(properties);
        Message msg = MessageBuilder.createMessage(message, messageHeaders);
        rabbitTemplate.setConfirmCallback(confirmCallback);
        rabbitTemplate.setReturnCallback(returnCallback);
        CorrelationData cd = new CorrelationData();
        cd.setId(UUID.randomUUID().toString()); //全局唯一 id+时间戳
        //routingKey 是 匹配 # *
        rabbitTemplate.convertAndSend("exchange-2", "springboot.hello.123.hello", msg, cd);

    }


    public void send2(Object message, Map<String, Object> properties) throws Exception {
        log.info("RabbitSender  --  send  测试 交换机是 direct 类型 ");
        MessageHeaders messageHeaders = new MessageHeaders(properties);
        Message msg = MessageBuilder.createMessage(message, messageHeaders);
        rabbitTemplate.setConfirmCallback(confirmCallback);
        rabbitTemplate.setReturnCallback(returnCallback);
        CorrelationData cd = new CorrelationData();
        cd.setId(UUID.randomUUID().toString()); //全局唯一 id+时间戳
        rabbitTemplate.convertAndSend("exchange-3", "springboot.abc.#", msg, cd);

    }


    public void sendOrder3(Object message, Map<String, Object> properties) throws Exception {
        log.info("RabbitSender  --  send  测试 交换机是 direct 类型 自定义消息体");
        MessageHeaders messageHeaders = new MessageHeaders(properties);
        Message msg = MessageBuilder.createMessage(message, messageHeaders);
        rabbitTemplate.setConfirmCallback(confirmCallback);
        rabbitTemplate.setReturnCallback(returnCallback);
        CorrelationData cd = new CorrelationData();
        cd.setId(UUID.randomUUID().toString()); //全局唯一 id+时间戳
        rabbitTemplate.convertAndSend("exchange-4", "springboot.abc.direct", msg, cd);

    }


}
